import 'package:flutter/material.dart';
import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:hm_show_flutter/api/service/home_service.dart';
import 'package:hm_show_flutter/constant/string.dart';
import 'package:hm_show_flutter/entity/banner_entity.dart';
import 'package:hm_show_flutter/event/category_event.dart';
import 'package:hm_show_flutter/event/login_event.dart';
import 'package:hm_show_flutter/page/category/category_View.dart';
import 'package:hm_show_flutter/page/home/home_view.dart';
import 'package:hm_show_flutter/page/home/swiper_view.dart';
import 'package:hm_show_flutter/page/mine/mine_view.dart';
import 'package:hm_show_flutter/style/style.dart';
import 'package:hm_show_flutter/utils/navigator_util.dart';
import 'package:hm_show_flutter/utils/toast_util.dart';

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  int _selectedIndex = 0;
  static const TextStyle optionStyle =
      TextStyle(fontSize: 18, fontWeight: FontWeight.bold);
  List<Widget> _list = List();

  _loginEvent() {
    loginEventBus.on<LoginEvent>().listen((LoginEvent loginEvent) {
      if (!loginEvent.isLogin) {
        NavigatorUtils.goLogin(context);
      }
    });
  }

  @override
  void initState() {
    super.initState();
    _list..add(HomeView())..add(CategoryView())..add(MineView());
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
      if (_selectedIndex == 1) {
        categoryEventBus.fire(CategoryEvent());
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    _loginEvent();

    DateTime _lastPopTime;
    _doubleBack() async {
      // 点击返回键的操作
      if (_lastPopTime == null ||
          DateTime.now().difference(_lastPopTime) > Duration(seconds: 2)) {
        _lastPopTime = DateTime.now();
        ToastUtil.showToast('再按一次退出');
      } else {
        _lastPopTime = DateTime.now();
        // 退出app
        await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
      }
    }

    return WillPopScope(
      onWillPop: _doubleBack,
      child: Scaffold(
        body: IndexedStack(
          index: _selectedIndex,
          children: _list,
        ),
        bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.home),
              title: Text(Strings.MAIN_HOME),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.category),
              title: Text(Strings.MAIN_CATEGORY),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.person),
              title: Text(Strings.MAIN_MINE),
            ),
          ],
          currentIndex: _selectedIndex,
          selectedItemColor: AppColors.mainColor,
          unselectedItemColor: Colors.grey,
          onTap: _onItemTapped,
        ),
      ),
    );
  }
}
